package com.polaris.live.widget.country

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.RelativeLayout
import com.polaris.live.R
import com.polaris.live.databinding.WidgetCountryBinding
import com.polaris.live.utils.CommonUtils

/**
 * 国家控件
 *
 * @author Created by lucas on 2023/10/22 16:32
 * @since 1.0.0
 */
class CountryView(context: Context, attributeSet: AttributeSet) :
    RelativeLayout(context, attributeSet) {

    private val mBinding = WidgetCountryBinding.inflate(LayoutInflater.from(context), this)

    /**
     * 设置国家
     */
    fun setCountry(countryCode: String?) {
        //适配以前的用户   有的用户之前因为没做限制导致以字符串的null传到了服务端
        val isNull = countryCode == "null" || countryCode == "NULL"
        if (countryCode != null && !isNull) {
            visibility = VISIBLE

            val countryIcon = countryIcons[countryCode]
            if (countryIcon == null) {
                mBinding.ivIcon.visibility = GONE
            } else {
                mBinding.ivIcon.visibility = VISIBLE
                mBinding.ivIcon.setImageResource(countryIcon)
            }

            mBinding.tvCountry.text = CommonUtils.getCountryName(countryCode)
        } else {
            visibility = GONE
        }
    }
}

val countryIcons = mapOf(
    "AF" to R.drawable.icon_country_af,
    "LU" to R.drawable.icon_country_lu,
    "AR" to R.drawable.icon_country_ar,
    "CC" to R.drawable.icon_country_cc,
    "VG" to R.drawable.icon_country_vg,
    "JO" to R.drawable.icon_country_jo,
    "PF" to R.drawable.icon_country_pf,
    "LC" to R.drawable.icon_country_lc,
    "SC" to R.drawable.icon_country_sc,
    "BF" to R.drawable.icon_country_bf,
    "BN" to R.drawable.icon_country_bn,
    "NR" to R.drawable.icon_country_nr,
    "BL" to R.drawable.icon_country_bl,
    "IN" to R.drawable.icon_country_in,
    "CO" to R.drawable.icon_country_co,
    "TW" to R.drawable.icon_country_tw,
    "MN" to R.drawable.icon_country_mn,
    "TT" to R.drawable.icon_country_tt,
    "MM" to R.drawable.icon_country_mm,
    "NA" to R.drawable.icon_country_na,
    "LT" to R.drawable.icon_country_lt,
    "KW" to R.drawable.icon_country_kw,
    "NP" to R.drawable.icon_country_np,
    "RU" to R.drawable.icon_country_ru,
    "KG" to R.drawable.icon_country_kg,
    "TZ" to R.drawable.icon_country_tz,
    "UG" to R.drawable.icon_country_ug,
    "TC" to R.drawable.icon_country_tc,
    "CH" to R.drawable.icon_country_ch,
    "CN" to R.drawable.icon_country_cn,
    "EE" to R.drawable.icon_country_ee,
    "RO" to R.drawable.icon_country_ro,
    "MK" to R.drawable.icon_country_mk,
    "BW" to R.drawable.icon_country_bw,
    "TV" to R.drawable.icon_country_tv,
    "LA" to R.drawable.icon_country_la,
    "BG" to R.drawable.icon_country_bg,
    "US" to R.drawable.icon_country_us,
    "LS" to R.drawable.icon_country_ls,
    "FJ" to R.drawable.icon_country_fj,
    "JM" to R.drawable.icon_country_jm,
    "BO" to R.drawable.icon_country_bo,
    "GB" to R.drawable.icon_country_gb,
    "SL" to R.drawable.icon_country_sl,
    "RS" to R.drawable.icon_country_rs,
    "UY" to R.drawable.icon_country_uy,
    "AU" to R.drawable.icon_country_au,
    "AQ" to R.drawable.icon_country_au,
    "FI" to R.drawable.icon_country_fi,
    "SI" to R.drawable.icon_country_si,
    "ST" to R.drawable.icon_country_st,
    "GL" to R.drawable.icon_country_gl,
    "AE" to R.drawable.icon_country_ae,
    "MR" to R.drawable.icon_country_mr,
    "CU" to R.drawable.icon_country_cu,
    "IO" to R.drawable.icon_country_io,
    "ET" to R.drawable.icon_country_et,
    "FR" to R.drawable.icon_country_fr,
    "PN" to R.drawable.icon_country_pn,
    "AT" to R.drawable.icon_country_at,
    "CK" to R.drawable.icon_country_ck,
    "SY" to R.drawable.icon_country_sy,
    "VI" to R.drawable.icon_country_vi,
    "FM" to R.drawable.icon_country_fm,
    "AS" to R.drawable.icon_country_as,
    "GI" to R.drawable.icon_country_gi,
    "LV" to R.drawable.icon_country_lv,
    "CM" to R.drawable.icon_country_cm,
    "GQ" to R.drawable.icon_country_gq,
    "CV" to R.drawable.icon_country_cv,
    "PK" to R.drawable.icon_country_pk,
    "SK" to R.drawable.icon_country_sk,
    "NC" to R.drawable.icon_country_nc,
    "BR" to R.drawable.icon_country_br,
    "MH" to R.drawable.icon_country_mh,
    "CX" to R.drawable.icon_country_cx,
    "BT" to R.drawable.icon_country_bt,
    "LK" to R.drawable.icon_country_lk,
    "GT" to R.drawable.icon_country_gt,
    "GM" to R.drawable.icon_country_gm,
    "MF" to R.drawable.icon_country_mf,
    "BI" to R.drawable.icon_country_bi,
    "HN" to R.drawable.icon_country_hn,
    "DO" to R.drawable.icon_country_do,
    "VU" to R.drawable.icon_country_vu,
    "ML" to R.drawable.icon_country_ml,
    "FK" to R.drawable.icon_country_fk,
    "GA" to R.drawable.icon_country_ga,
    "DK" to R.drawable.icon_country_dk,
    "PR" to R.drawable.icon_country_pr,
    "NF" to R.drawable.icon_country_nf,
    "PL" to R.drawable.icon_country_pl,
    "KR" to R.drawable.icon_country_kr,
    "LY" to R.drawable.icon_country_ly,
    "TN" to R.drawable.icon_country_tn,
    "SM" to R.drawable.icon_country_sm,
    "ME" to R.drawable.icon_country_me,
    "MW" to R.drawable.icon_country_mw,
    "YE" to R.drawable.icon_country_ye,
    "CS" to R.drawable.icon_country_cs,
    "IT" to R.drawable.icon_country_it,
    "MQ" to R.drawable.icon_country_mq,
    "AM" to R.drawable.icon_country_am,
    "AW" to R.drawable.icon_country_aw,
    "SN" to R.drawable.icon_country_sn,
    "RW" to R.drawable.icon_country_rw,
    "TM" to R.drawable.icon_country_tm,
    "TJ" to R.drawable.icon_country_tj,
    "HK" to R.drawable.icon_country_hk,
    "BM" to R.drawable.icon_country_bm,
    "NZ" to R.drawable.icon_country_nz,
    "MO" to R.drawable.icon_country_mo,
    "KE" to R.drawable.icon_country_ke,
    "VE" to R.drawable.icon_country_ve,
    "PE" to R.drawable.icon_country_pe,
    "KM" to R.drawable.icon_country_km,
    "BA" to R.drawable.icon_country_ba,
    "LB" to R.drawable.icon_country_lb,
    "OM" to R.drawable.icon_country_om,
    "SE" to R.drawable.icon_country_se,
    "SZ" to R.drawable.icon_country_sz,
    "QA" to R.drawable.icon_country_qa,
    "IS" to R.drawable.icon_country_is,
    "MT" to R.drawable.icon_country_mt,
    "NO" to R.drawable.icon_country_no,
    "AI" to R.drawable.icon_country_ai,
    "MG" to R.drawable.icon_country_mg,
    "KY" to R.drawable.icon_country_ky,
    "DZ" to R.drawable.icon_country_dz,
    "SB" to R.drawable.icon_country_sb,
    "UA" to R.drawable.icon_country_ua,
    "AL" to R.drawable.icon_country_al,
    "SX" to R.drawable.icon_country_sx,
    "AD" to R.drawable.icon_country_ad,
    "WF" to R.drawable.icon_country_wf,
    "PM" to R.drawable.icon_country_pm,
    "GW" to R.drawable.icon_country_gw,
    "AZ" to R.drawable.icon_country_az,
    "IR" to R.drawable.icon_country_ir,
    "ZM" to R.drawable.icon_country_zm,
    "MY" to R.drawable.icon_country_my,
    "GU" to R.drawable.icon_country_gu,
    "BY" to R.drawable.icon_country_by,
    "CI" to R.drawable.icon_country_ci,
    "EG" to R.drawable.icon_country_eg,
    "TH" to R.drawable.icon_country_th,
    "SA" to R.drawable.icon_country_sa,
    "BZ" to R.drawable.icon_country_bz,
    "AO" to R.drawable.icon_country_ao,
    "GH" to R.drawable.icon_country_gh,
    "SO" to R.drawable.icon_country_so,
    "MX" to R.drawable.icon_country_mx,
    "ES" to R.drawable.icon_country_es,
    "TG" to R.drawable.icon_country_tg,
    "PA" to R.drawable.icon_country_pa,
    "BS" to R.drawable.icon_country_bs,
    "MZ" to R.drawable.icon_country_mz,
    "ER" to R.drawable.icon_country_er,
    "KN" to R.drawable.icon_country_kn,
    "PT" to R.drawable.icon_country_pt,
    "HT" to R.drawable.icon_country_ht,
    "MS" to R.drawable.icon_country_ms,
    "KZ" to R.drawable.icon_country_kz,
    "JP" to R.drawable.icon_country_jp,
    "GE" to R.drawable.icon_country_ge,
    "UZ" to R.drawable.icon_country_uz,
    "BH" to R.drawable.icon_country_bh,
    "BJ" to R.drawable.icon_country_bj,
    "HR" to R.drawable.icon_country_hr,
    "ZA" to R.drawable.icon_country_za,
    "KP" to R.drawable.icon_country_kp,
    "ID" to R.drawable.icon_country_id,
    "SR" to R.drawable.icon_country_sr,
    "AG" to R.drawable.icon_country_ag,
    "NG" to R.drawable.icon_country_ng,
    "TO" to R.drawable.icon_country_to,
    "CG" to R.drawable.icon_country_cg,
    "BB" to R.drawable.icon_country_bb,
    "PY" to R.drawable.icon_country_py,
    "EC" to R.drawable.icon_country_ec,
    "DJ" to R.drawable.icon_country_dj,
    "BE" to R.drawable.icon_country_be,
    "GP" to R.drawable.icon_country_gp,
    "CA" to R.drawable.icon_country_ca,
    "GG" to R.drawable.icon_country_gg,
    "CZ" to R.drawable.icon_country_cz,
    "DE" to R.drawable.icon_country_de,
    "IM" to R.drawable.icon_country_im,
    "PG" to R.drawable.icon_country_pg,
    "CY" to R.drawable.icon_country_cy,
    "IE" to R.drawable.icon_country_ie,
    "BD" to R.drawable.icon_country_bd,
    "ZW" to R.drawable.icon_country_zw,
    "MP" to R.drawable.icon_country_mp,
    "TR" to R.drawable.icon_country_tr,
    "HU" to R.drawable.icon_country_hu,
    "SV" to R.drawable.icon_country_sv,
    "GF" to R.drawable.icon_country_gf,
    "MA" to R.drawable.icon_country_ma,
    "TL" to R.drawable.icon_country_tl,
    "IQ" to R.drawable.icon_country_iq,
    "KI" to R.drawable.icon_country_ki,
    "NL" to R.drawable.icon_country_nl,
    "NU" to R.drawable.icon_country_nu,
    "MD" to R.drawable.icon_country_md,
    "RE" to R.drawable.icon_country_re,
    "MU" to R.drawable.icon_country_mu,
    "MC" to R.drawable.icon_country_mc,
    "GY" to R.drawable.icon_country_gy,
    "CW" to R.drawable.icon_country_cw,
    "WS" to R.drawable.icon_country_ws,
    "VA" to R.drawable.icon_country_va,
    "PH" to R.drawable.icon_country_ph,
    "SS" to R.drawable.icon_country_ss,
    "LR" to R.drawable.icon_country_lr,
    "AX" to R.drawable.icon_country_ax,
    "NI" to R.drawable.icon_country_ni,
    "YT" to R.drawable.icon_country_yt,
    "XK" to R.drawable.icon_country_xk,
    "JE" to R.drawable.icon_country_je,
    "CF" to R.drawable.icon_country_cf,
    "KH" to R.drawable.icon_country_kh,
    "VC" to R.drawable.icon_country_vc,
    "TK" to R.drawable.icon_country_tk,
    "CL" to R.drawable.icon_country_cl,
    "NE" to R.drawable.icon_country_ne,
    "VN" to R.drawable.icon_country_vn,
    "PW" to R.drawable.icon_country_pw,
    "SD" to R.drawable.icon_country_sd,
    "GR" to R.drawable.icon_country_gr,
    "CD" to R.drawable.icon_country_cd,
    "FO" to R.drawable.icon_country_fo,
    "DM" to R.drawable.icon_country_dm,
    "GN" to R.drawable.icon_country_gn,
    "LI" to R.drawable.icon_country_li,
    "TD" to R.drawable.icon_country_td,
    "GD" to R.drawable.icon_country_gd,
    "SG" to R.drawable.icon_country_sg,
    "CR" to R.drawable.icon_country_cr,
    "SH" to R.drawable.icon_country_sh,
    "MV" to R.drawable.icon_country_mv,
    "unknow" to R.drawable.icon_country_other
)